﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Winista.Text.HtmlParser;
using Winista.Text.HtmlParser.Extractors;
using Winista.Text.HtmlParser.Nodes;
using Winista.Text.HtmlParser.Util;
using Winista.Text.HtmlParser.Tags;
using Winista.Text.HtmlParser.Visitors;
using Winista.Text.HtmlParser.Filters;
using System.Web;


namespace seo.util
{
    public class GetContent
    {
        private const int standard_length = 200;
        private static Stack<string> stack = new Stack<string>();
        private static string maxStr = String.Empty;

        public string getTitle(string html)
        {
            string title = String.Empty;
            Parser parser = Parser.CreateParser(html, "utf-8");
            HtmlPage page = new HtmlPage(parser);
            parser.VisitAllNodesWith(page);
            title = page.Title;
            return title;
        }

        public string getContentUsingParser(String html)
        {
            string htmlstr = string.Empty;
            try
            {
                  Parser parser = Parser.CreateParser(html,"utf-8");

                  HtmlPage page = new HtmlPage(parser);
                  parser.VisitAllNodesWith(page);
                  NodeList list = page.Body;


                  NodeFilter[] filters = new NodeFilter[3];
                  filters[0] = new NodeClassFilter(typeof(ParagraphTag));
                  filters[1] = new NodeClassFilter(typeof(TableTag));
                  filters[2] = new NodeClassFilter(typeof(Div));

                  OrFilter filter = new OrFilter();
                  filter.Predicates = filters;

                  //list = parser.ExtractAllNodesThatMatch(filter);
                  maxStr = String.Empty;
                  processNodeList(list, "trip",ref htmlstr);

                 // for (int i = 0; i < list.Size(); i++)
                 // {
                      //Console.WriteLine(list.ElementAt(i).ToHtml());
                 //     htmlstr = htmlstr + list.ElementAt(i).ToHtml();
                 // }   
                  //filter
                  
                  //p.ExtractAllNodesThatAre(BodyTag);
                  //p.ExtractAllNodesThatAr
                // nl = p.parse(new NodeClassFilter(BodyTag.class));    
                // BodyTag bt = (BodyTag) nl.elementAt(0);    
                // System.out.println(bt.toPlainTextString());    
                  
            }
            catch (ParserException e)
            {
                //e.printStackTrace();    
            }
            if (maxStr.Length < standard_length) maxStr = htmlstr;
            maxStr = HttpUtility.HtmlDecode(maxStr);
            return maxStr;
        }


        public static void processNodeList(NodeList list, String keyword, ref string htmlstr) 
        { 
		//迭代开始
		ISimpleNodeIterator iterator = list.Elements();
		while (iterator.HasMoreNodes()) {
			INode node = iterator.NextNode();
			//得到该节点的子节点列表
			NodeList childList = node.Children;
            //if (!(node is ParagraphTag || node is Div || node is TableTag || node is TableRow || node is TableColumn || node is Span)) childList = null;
			//孩子节点为空，说明是值节点
			if (null == childList)
			{
				//得到值节点的值
                if (!(node.Parent is ParagraphTag || node.Parent is Div || node.Parent is TableTag || node.Parent is TableRow || node.Parent is TableColumn || node.Parent is Span)) continue;
                //if (node.ToHtml().Contains("<")) continue;
				String result = node.ToPlainTextString();
                if (node.ToHtml().Contains("<")) result = node.ToHtml();

                //result = node.ToHtml();
				//若包含关键字，则简单打印出来文本
                //if (result.IndexOf(keyword) != -1)
                //if(result.Trim().Length > 200)
                htmlstr = htmlstr + result;
			} //end if
			//孩子节点不为空，继续迭代该孩子节点
			else 
			{
                if (node is Div || node is TableColumn)
                {
                    stack.Push(htmlstr);
                    htmlstr = String.Empty;
                }
				processNodeList(childList, keyword,ref htmlstr);
                if (node is Div || node is TableColumn)
                {
                    if (htmlstr.Length > maxStr.Length) maxStr = htmlstr;
                    htmlstr = stack.Pop();
                }
			}//end else
		}//end wile
        
        
        }
    }
}
